import os
import sys
import requests
# If you are using a Jupyter notebook, uncomment the following line.
%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO

# Add your Computer Vision subscription key and endpoint to your environment variables.
#if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
   # subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
#else:
   # print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
    #sys.exit()

#if 'COMPUTER_VISION_ENDPOINT' in os.environ:
 #   endpoint = os.environ['COMPUTER_VISION_ENDPOINT']

landmark_analyze_url = "https://cjn813.cognitiveservices.azure.com/vision/v3.1/models/landmarks/analyze"

# Set image_url to the URL of an image that you want to analyze.
image_url = "https://gitee.com/chen_jia_nan/wx923/raw/master/pictures/tiananmen.jpg"
headers = {'Ocp-Apim-Subscription-Key': 'e33e05cf8a914c73b9a740cab41e61cc'}
params = {'model': 'landmarks'}
data = {'url': image_url}
response = requests.post(
    landmark_analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()

# The 'analysis' object contains various fields that describe the image. The
# most relevant landmark for the image is obtained from the 'result' property.
analysis = response.json()
assert analysis["result"]["landmarks"] is not []
print(analysis)
landmark_name = analysis["result"]["landmarks"][0]["name"].capitalize()

# Display the image and overlay it with the landmark name.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(landmark_name, size="x-large", y=-0.1)
plt.show()